package com.jtpay.sdk.activity;

import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jtpay.sdk.R;
import com.jtpay.sdk.base.BaseActivity;
import com.jtpay.sdk.weight.AppNamePop;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import apiUtils.models.OrderModel;
import apiUtils.models.PayAppInfo;
import apiUtils.models.PayInfo;
import appUtils.ToastUtil;
import butterknife.Bind;
import butterknife.OnClick;


public class OrderActivity extends BaseActivity {
    @Bind(R.id.layout_title_textName)
    TextView mTitleTextView;
    @Bind(R.id.app_name_textview)
    TextView mAppNameTv;
    @Bind(R.id.orderid_tv)
    TextView mOrderidTv;
    @Bind(R.id.money_et)
    EditText mMoneyEt;
    @Bind(R.id.submit_btn)
    Button mSubmitBtn;

    private List<PayAppInfo> mPayAppInfos = new ArrayList<>();
    private String mMoney = "0.10";
    private AppNamePop mPopMenu;
    private PayAppInfo mPayAppInfo;

    @Override
    protected void initView() {
        mTitleTextView.setText(R.string.Create_the_order);
        mMoneyEt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        mMoneyEt.setText(mMoney);
        mPopMenu = new AppNamePop(mPayAppInfos, this);
        // Menu item click listener
        mPopMenu.setOnItemClickListener(popmenuItemClickListener);
        if (mPayAppInfos != null && !mPayAppInfos.isEmpty()) {
            mPayAppInfo = mPayAppInfos.get(0);
            initOrder(mPayAppInfo.getAppName());
        }
    }

    // Popup menu listener
    OnItemClickListener popmenuItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            mPayAppInfo = mPayAppInfos.get(position);
            initOrder(mPayAppInfo.getAppName());
            mPopMenu.dismiss();
        }
    };

    private void initOrder(String appName) {
        mAppNameTv.setText(appName);
        initOrderNumber();
    }

    private void initOrderNumber() {
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        mOrderidTv.setText(dateFormat.format(date));
    }

    @Override
    protected void onStart() {
        super.onStart();
        initOrderNumber();
    }

    @Override
    protected void initData() {
        mPayAppInfos.addAll(PayInfo.getInstance().getPayAppInfos());
    }

    @OnClick({R.id.submit_btn, R.id.layout_title_iv_left, R.id.app_name_textview})
    void buttonClick(View view) {
        switch (view.getId()) {
            case R.id.submit_btn:
                startActivity();
                break;
            case R.id.layout_title_iv_left:
                backAction();
                break;
            case R.id.app_name_textview:
                mPopMenu.showAsDropDown(view);
                break;
            default:

                break;
        }
    }

    private void backAction() {
        finish();
    }

    @Override
    public int setView() {
        return R.layout.activity_order;
    }

    private void startActivity() {
        String orderMoney = mMoneyEt.getText().toString();
        String appName = mAppNameTv.getText().toString();
        String orderId = mOrderidTv.getText().toString();
        if (TextUtils.isEmpty(appName)) {
            ToastUtil.showShort(getString(R.string.please_select_appname));
        } else if (TextUtils.isEmpty(orderMoney)) {
            ToastUtil.showShort(getString(R.string.please_enter_money));
        } else {
            PayInfo.getInstance().setOrderModel(new OrderModel(appName, mPayAppInfo.getAppid(), orderId, orderMoney));
            PayInfo.getInstance().setAppId(mPayAppInfo.getAppid());
            PayInfo.getInstance().setAppKey(mPayAppInfo.getAppKey());
            PayInfo.getInstance().setKeyAES(mPayAppInfo.getAppAESKey());
            PayInfo.getInstance().setVectorAES(mPayAppInfo.getAppAesVector());
            Intent intent = new Intent(OrderActivity.this, ProductActivity.class);
            startActivity(intent);
        }
    }
}
